package platform;

import common.Singleton;
import view.ViewControl;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.util.ArrayList;

import javax.swing.JPanel;
import javax.swing.border.MatteBorder;

/**
 * 意象平台。平台用来放置并显示意象链 主意象展开或收起时，平台会自动改变其占用空间以正常显示意象链 每个意象链使用一个平台。
 *
 * @author chai
 *
 */
public class MeanPlatform extends JPanel {
    private ViewControl vc;

    private static final long serialVersionUID = 1L;
    private final int defaultWidth = 265, defaultHeight = MeanLabel.defaultHeight;
    private MeanLabel label;
    private ArrayList<MeanPlatform> vice;// 副平台;
    private MeanPlatform boss;

    private int requiredWidth = 0, requiredHeight = 0, height = defaultHeight;
    private Color origin;

    private MeanPlatform() {
        vc = Singleton.vc();
    }
    // rW是总长度，rH是总高度，height是当前高度
    public MeanPlatform(MeanLabel label) {
        this();
        setOrigin(new Color(250, 250, 200));
        this.label = label;
        add(label);
        label.setGroup_plat(this);

        setBorder(new MatteBorder(0, 0, 1, 0, Color.LIGHT_GRAY));

        int tempLine = 0, lines = 1;
        tempLine += label.getPreferredSize().width;
        for (MeanLabel next : label.offsets) {
            if (next.offsets != null) {
                if (vice == null)
                    vice = new ArrayList<>();
                MeanPlatform temp = new MeanPlatform(next);
                temp.setOrigin(new Color(220, 255, 220));
                temp.boss = this;
                vice.add(temp);
                add(temp);
            } else {
                int nextWidth = next.getPreferredSize().width;
                tempLine += nextWidth;
                if (tempLine >= defaultWidth - 5) {
                    tempLine = nextWidth;
                    lines++;
                }
                requiredWidth += next.getPreferredSize().getWidth();
                add(next);
            }
        }
        requiredHeight += defaultHeight * lines;
        vc.requiredHeight += requiredHeight;
        setPreferredSize(new Dimension(defaultWidth, height));
        setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
    }

    public void reSetVisible() {
        if (boss != null)
            boss.reSet_visible();
        else
            reSet_visible();
    }

    private void reSet_visible() {
        if (label.offsetsVisible) {
            height = requiredHeight;
        } else {
            height = defaultHeight;
            vc.reSetAttrHeight();
            setPreferredSize(new Dimension(defaultWidth, height));
            // 副Platform以下程序没有意义，主Platform此时禁止以下程序
            return;
        }
        if (vice != null) {
            for (MeanPlatform next : vice) {
                next.reSet_visible();
                height += next.height;
            }
        }
        vc.reSetAttrHeight();
        setPreferredSize(new Dimension(defaultWidth, height));
    }

    public void removePlat(MeanPlatform plat) {
        vice.remove(plat);
        remove(plat);
        reSetVisible();
    }

    public void removeAttr(MeanLabel attr) {
        attr.setVisible(false);
        remove(attr);
        reSetHeight();
    }

    private void reSetHeight() {
        requiredWidth = 0;
        int tempLine = 0, lines = 1;
        tempLine += label.getPreferredSize().width;
        for (MeanLabel next : label.offsets) {
            if (next.offsets != null)
                continue;
            int nextWidth = next.getPreferredSize().width;
            tempLine += nextWidth;
            if (tempLine >= defaultWidth - 5) {
                tempLine = nextWidth;
                lines++;
            }
            requiredWidth += next.getPreferredSize().getWidth();
            add(next);
        }
        requiredHeight = defaultHeight * lines;
        reSetVisible();
    }

    public int getHeight() {
        return height;
    }

    public void beSelected(boolean selected) {
        if (vice != null) {
            for (MeanPlatform aVice : vice) {
                aVice.beSelected(selected);
            }
        }
        if (selected)
            setBackground(label.getBackground());
        else
            setBackground(origin);
    }

    private void setOrigin(Color color) {
        origin = color;
        setBackground(color);
    }
}
